package com.android.monee.data.access.json;

import java.util.ArrayList;
import java.util.List;

import com.android.monee.data.access.MensualityDao;
import com.android.monee.data.access.inMemory.DaoUtils;
import com.android.monee.data.model.Budget;
import com.android.monee.data.model.Mensuality;
import com.android.monee.view.activity.MoneeCtx;

public class MensualityDaoJson extends AbstractObservableDaoJson<Mensuality, Integer> implements MensualityDao{

	private static MensualityDaoJson instance;
	
	public static MensualityDaoJson getInstance(){
		if(instance == null){
			instance = new MensualityDaoJson();
			instance.init(MoneeCtx.rootDir, Mensuality.class, Integer.class);
		}
		
		return instance;
	}
	
	@Override
	public void insert(Mensuality obj) {
		Mensuality mens = new Mensuality(obj);
		if(mens.getBudgetId()==null)
			throw new RuntimeException("budget id must not be null !");//TODO tester cela 
		super.insert(mens);
		obj.setId(mens.getId());
	}


	@Override
	public void update(Mensuality obj) {
		Mensuality mens = new Mensuality(obj);
		if(mens.getBudgetId()==null)
			throw new RuntimeException("budget id must not be null !");
		super.update(mens);
	}


	@Override
	protected Integer getNextId() {
		return DaoUtils.getNextId(this.datas.values());
	}

	@Override
	public List<Mensuality> findByBudget(Budget budget) {
		List<Mensuality> res = new ArrayList<Mensuality>();
		
		for(Mensuality mens : super.datas.values()){
			if(mens.getBudgetId().equals(budget.getId()))
				res.add(mens);
		}
		return res;
	}


//	@Override
//	public List<Mensuality> findByBudgetAndState(Budget budget,
//			MensualityState state) {
//		List<Mensuality> res = new ArrayList<Mensuality>();
//		
//		for(Mensuality mens : super.datas.values()){
//			if(mens.getBudgetId().equals(budget.getId()) 
//					&& mens.getState().equals(state))
//				res.add(mens);
//		}
//		return res;
//	}
}
